// mircoApp/gradevin/inventoryDetail/inventoryDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stock: [],
    list: [],
    page: 1,
    isLoadMore: false,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log("没有更多了");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.index.stock-log");
    app._getNetWork({
      url: urlStr,
      data: {
        goods_id: this.options.id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: res.data.list.data,
            stock: res.data.stock
          })
          this.data.isLoadMore = true;
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      wx.showLoading({
        title: "正在加载",
      });
      this.setData({
        page: this.data.page + 1,
      });
      urlStr = app.getNetAddresss(
        "plugin.gradevin-stock.frontend.controllers.index.stock-log"
      );
      app._postNetWork({
        url: urlStr,
        data: {
          goods_id: this.options.id,
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.list.data),
            });
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res);
        },
      });
    }
  },
})